package com.itbaizhan.shopping_goods_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.ProductType;
import com.itbaizhan.shopping_common.result.BusException;
import com.itbaizhan.shopping_common.result.CodeEnum;
import com.itbaizhan.shopping_common.service.ProductTypeService;
import com.itbaizhan.shopping_goods_service.mapper.ProductTypeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
@Transactional
public class ProductTypeImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public void add(ProductType productType) {
        //获取上级类型id
        long parentId = productType.getParentId();
        ProductType parentType = productTypeMapper.selectById(parentId);
        if (parentType == null) { //没有父级别
            productType.setLevel(1);
        } else if(parentType.getLevel() < 3) {
            productType.setLevel(parentType.getLevel() + 1);
        } else if(parentType.getLevel() == 3) {
            throw new BusException(CodeEnum.INSERT_GOODS_TYPE_ERROR);
        }
        productTypeMapper.insert(productType);
    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public void update(ProductType productType) {
        //获取上级类型id
        long parentId = productType.getParentId();
        ProductType parentType = productTypeMapper.selectById(parentId);
        if (parentType == null) { //没有父级别
            productType.setLevel(1);
        } else if(parentType.getLevel() < 3) {
            productType.setLevel(parentType.getLevel() + 1);
        } else if(parentType.getLevel() == 3) {
            throw new BusException(CodeEnum.INSERT_GOODS_TYPE_ERROR);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public Page<ProductType> search(ProductType productType, int page, int size) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        if (productType != null && StringUtils.hasText(productType.getName())) {
            wrapper.like("name", productType.getName());
        }
        if(productType != null && productType.getParentId() != null){
            wrapper.eq("parentId", productType.getParentId());
        }
        return productTypeMapper.selectPage(new Page<>(page, size), wrapper);
    }

    @Override
    public List<ProductType> findAll(ProductType productType) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        if (productType != null && StringUtils.hasText(productType.getName())) {
            wrapper.like("name", productType.getName());
        }
        if(productType != null && productType.getParentId() != null){
            wrapper.eq("parentId", productType.getParentId());
        }
        return productTypeMapper.selectList(wrapper);
    }


    @Override
    public void delete(Long id) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId", id);
        ProductType productType = productTypeMapper.selectOne(wrapper);
        if (productType != null) { //有子类型，不能删除
            throw new BusException(CodeEnum.DELETE_GOODS_TYPE_ERROR);
        }
        productTypeMapper.deleteById(id);
    }
}
